//
//  JLCFApplicationEntrace.m
//  Jlcf_ios
//
//  Created by Ji on 2018/9/13.
//  Copyright © 2018年 ski. All rights reserved.
//

#import "JinLiCaiFuApplicationEntrace.h"
#import "JLCFApp.h"
#import "JLCFHttpManager.h"

//umeng
#import <UMSocialCore/UMSocialCore.h>
//引导
#import "QTZGuideView.h"

#import "GSKeyChainDataManager.h"

#import <AdSupport/AdSupport.h>

#import "JLCFBaseWebViewController.h"

@interface JinLiCaiFuApplicationEntrace ()<UIApplicationDelegate>
{
}

@property (nonatomic, strong) NSDictionary *remoteInfo;

@end

@implementation JinLiCaiFuApplicationEntrace

+ (JinLiCaiFuApplicationEntrace *)shareEntrance
{
    
    static JinLiCaiFuApplicationEntrace *entrance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        entrance = [[JinLiCaiFuApplicationEntrace alloc] init];
    });
    return entrance;
    
}

- (void)applicationEntrance:(UIWindow *)mainWindow launchOptions:(NSDictionary *)launchOptions
{
    self.window = mainWindow;
    
    //标记APP
    [JLCFApp registerApp:APP_NAME type:APP_TYPE channel:CHANNEL version:APP_VERSION appleId:APPLEID];
    
    //UUID
    [GSKeyChainDataManager saveUUID:[[UIDevice currentDevice].identifierForVendor UUIDString]];
    DBG(@"uuid : %@, idfa : %@", [[UIDevice currentDevice].identifierForVendor UUIDString], [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]);
    
    
    //uMeng
#ifdef DEBUG
    /* 打开调试日志 */
    [[UMSocialManager defaultManager] openLog:YES];
    
//    [MobClick setLogEnabled:YES];
#endif
    /* 设置友盟appkey */
    [[UMSocialManager defaultManager] setUmSocialAppkey:@"5b987d45a40fa32894000060"];
    UMConfigInstance.appKey = @"5b987d45a40fa32894000060";
    UMConfigInstance.channelId = @"App Store";
    [MobClick startWithConfigure:UMConfigInstance];
    [self configUSharePlatforms];
    
    // 注册通知
    [self registerRemoteNotification];
    
    
    //主页面初始化
    [self initViewControllers];
    [self guideView];//引导页
    
    [[JLCFRegisterService shareService] loadDetailUserInfo];
    
    // 注册APNs - custom method - 开发者自定义的方法
    
    
#pragma mark ------end------
    //开启网络监听
    [JLCFHttpManager startMonitoringNetworkStatus];
    
    if ([[launchOptions allKeys] containsObject:UIApplicationLaunchOptionsRemoteNotificationKey]) {
        self.remoteInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    }
    
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}

- (void)configUSharePlatforms
{
    /* 设置微信的appKey和appSecret */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wx2fb7b8ce67d0e0b3" appSecret:@"dcb5fb2f868a67f4ca615937529e203c" redirectURL:@"http://mobile.umeng.com/social"];
    //    /*qq*/
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"1107812548" appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
}

- (void)registerRemoteNotification
{
    //    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
    
    UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:nil];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    [[UIApplication sharedApplication] registerUserNotificationSettings:uns];
    //    } else {
    //        UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge);
    //        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type];
    //    }
}


- (void)initViewControllers
{
    self.tabBarViewController = [[JLCFNormalTabBarController alloc] init];
    JLCFNavigationController *nav = [[JLCFNavigationController alloc] initWithRootViewController:self.tabBarViewController];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    
    self.currentNavController = nav;
}

- (void)guideView
{
    NSArray *guides = @[@"guide_1", @"guide_2", @"guide_3"];
    [QTZGuideView showGudieView:guides];
}

- (JLCFNavigationController *)currentNavController
{
    UIViewController *rootController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    //    if (self.currentNavController) {
    //        return self.currentNavController;
    //    }
    
    JLCFNavigationController *nav;
    if ([rootController isKindOfClass:[JLCFNavigationController class]]) {
        nav = (JLCFNavigationController *)rootController;
        self.currentNavController = nav;
    } else if ([rootController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabbarController = (UITabBarController *)rootController;
        if ([tabbarController.selectedViewController isKindOfClass:[JLCFNavigationController class]]) {
            nav = (JLCFNavigationController *)tabbarController.selectedViewController;
            self.currentNavController = nav;
        }
    }
    return nav;
}


- (void)popTopViewController:(NSString *)vcClass
{
    UIViewController *target = nil;
    for (UIViewController * controller in self.currentNavController.viewControllers) { //遍历
        if ([controller isKindOfClass:NSClassFromString(vcClass)]) { //这里判断是否为你想要跳转的页面
            target = controller;
        }
    }
    if (target) {
        [self.currentNavController popToViewController:target animated:YES]; //跳转
    }
}

- (BOOL)isContainVC:(NSString *)vc
{
    UIViewController *target = nil;
    for (UIViewController * controller in self.currentNavController.viewControllers) { //遍历
        if ([controller isKindOfClass:NSClassFromString(vc)]) { //这里判断是否为你想要跳转的页面
            target = controller;
        }
    }
    if (target) {
        return YES;
    } else {
        return NO;
    }
}

- (void)applicationRegisterDeviceToken:(NSData*)deviceToken
{
    
}

- (void)applicationFailToRegisterDeviceToken:(NSError*)error
{
    NSLog(@"\n>>>[DeviceToken Error]:%@\n\n", error.description);
}

- (void)applicationReceiveNotifaction:(NSDictionary*)userInfo
{
    self.remoteInfo = userInfo;
}

- (void)applicationDidReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    
}

- (void)handleRemoteInfo
{
    if (self.remoteInfo && [[self.remoteInfo allKeys] containsObject:@"payload"]) {
        
        NSString *url = [NSString stringWithFormat:@"%@",[self.remoteInfo objectForKey:@"payload"]];
        if (url && ![url isEqualToString:@"#"]) {
            [self toActiveViewWithUrl:url];
        }
        
        self.remoteInfo = nil;
        
    } else {
        
        self.remoteInfo = nil;
        
    }
    
}

- (void)toActiveViewWithUrl:(NSString *)url
{
    JLCFBaseWebViewController *web = [JLCFBaseWebViewController new];
    web.url = url;
    [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:web animated:YES];
}

@end
